package com.clannad.msmservice.controller;

import com.clannad.commonutils.R;
import com.clannad.msmservice.service.MsmService;
import com.clannad.msmservice.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/edumsm/msm")
//@CrossOrigin
@Slf4j
@Api( "验证码")
public class MsmController {
    @Autowired
    private MsmService service;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("send/{phone}")
    public R sendMsm(@PathVariable String phone){
        if(StringUtils.isEmpty(phone)){
            return R.error().message("手机号为空");
        }
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return R.ok();
        }
        String fourBitRandom = RandomUtil.getFourBitRandom();
        log.warn("手机号为：{}，本次的验证码为：{}，5分钟内有效，请勿泄露！",phone,fourBitRandom);
        redisTemplate.opsForValue().set(phone,fourBitRandom,5, TimeUnit.MINUTES);
        return R.ok();
    }
}
